package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.EmsDeviceCalConfig;
import com.econ.powercloud.ems.dal.mapper.EmsDeviceCalConfigMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author : zjf
 * @Description :
 * @Date : 2022/3/29 16:16
 * @Modified by : zjf
 */
@Repository
public class EmsDeviceCalConfigDAO extends ServiceImpl<EmsDeviceCalConfigMapper, EmsDeviceCalConfig> {
    private final static String LIMIT_ONE = "limit 1";

    public List<EmsDeviceCalConfig> selectListByLocations(List<Integer> locations){
        LambdaQueryWrapper<EmsDeviceCalConfig> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EmsDeviceCalConfig::getLocation,locations);
        List<EmsDeviceCalConfig> configList = this.baseMapper.selectList(lambdaQueryWrapper);
        return configList;
    }

    public EmsDeviceCalConfig selectOneByLocation(Integer location){
        LambdaQueryWrapper<EmsDeviceCalConfig> wrapper = Wrappers.<EmsDeviceCalConfig>lambdaQuery()
                .eq(EmsDeviceCalConfig::getLocation, location)
                .last(LIMIT_ONE);
        return this.getBaseMapper().selectOne(wrapper);
    }

    public List<EmsDeviceCalConfig> selectByLocation(Integer location){
        LambdaQueryWrapper<EmsDeviceCalConfig> wrapper = Wrappers.<EmsDeviceCalConfig>lambdaQuery()
                .eq(EmsDeviceCalConfig::getLocation, location)
                .orderByAsc(EmsDeviceCalConfig::getSorting);
        return this.list(wrapper);
    }

    public List<EmsDeviceCalConfig> selectListByLocationAndDeviceCode(String deviceCode, Integer location){
        LambdaQueryWrapper<EmsDeviceCalConfig> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(EmsDeviceCalConfig::getLocation,location);
        lambdaQueryWrapper.eq(EmsDeviceCalConfig::getDeviceCode,deviceCode);
        List<EmsDeviceCalConfig> configList = this.baseMapper.selectList(lambdaQueryWrapper);
        return configList;
    }

    public Integer deleteByDeviceCodes(List<String> deviceCodes){
        LambdaQueryWrapper<EmsDeviceCalConfig> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EmsDeviceCalConfig::getDeviceCode,deviceCodes);
        return this.baseMapper.delete(lambdaQueryWrapper);
    }

    public Integer deleteByComputeCodes(List<String> computeCodes){
        LambdaQueryWrapper<EmsDeviceCalConfig> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EmsDeviceCalConfig::getComputeCode,computeCodes);
        return this.baseMapper.delete(lambdaQueryWrapper);
    }
}
